package com.book.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.business.entity.BookLove;
import com.book.business.mapper.BookLoveMapper;
import com.book.business.service.BookLoveService;
import com.book.common.entity.QueryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 盛佳帅
 * @Description:  20173953
 */

@Service
public class BookLoveServiceImpl extends ServiceImpl<BookLoveMapper, BookLove> implements BookLoveService {

    @Autowired
    BookLoveMapper bookLoveMapper;

    @Override
    public List<BookLove> findBookLove(BookLove bookLove) {
        QueryWrapper<BookLove> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookLove.getPubid())) {
            queryWrapper.like("pubid", bookLove.getPubid());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookLove> pageBookLove(QueryRequest request, BookLove bookLove) {
        QueryWrapper<BookLove> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookLove.getBookname())) {
            queryWrapper.like("bookname", bookLove.getBookname());
        }
        if (StringUtils.isNotEmpty(bookLove.getType())) {
            queryWrapper.like("type", bookLove.getType());
        }
        if (StringUtils.isNotEmpty(bookLove.getUsername())) {
            queryWrapper.like("username", bookLove.getUsername());
        }
        Page<BookLove> bookLoveIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookLoveIPage, queryWrapper);
    }

    @Override
    public void addBookLove(BookLove bookLove) {
        this.save(bookLove);
    }

    @Override
    public void updateBookLove(BookLove bookLove) {
        this.saveOrUpdate(bookLove);
    }

    @Override
    public void deleteBookLove(String[] bookLove) {
        List<String> list = Arrays.asList(bookLove);
        this.removeByIds(list);
    }

    @Override
    public List<BookLove> payBookLove(String[] bookLove) {
        List<String> list = Arrays.asList(bookLove);
        return (List<BookLove>)listByIds(list);
    }
}